package com.woniuxy.caradmin.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Maintainrecord;
import com.woniuxy.caradmin.entity.pojo.Oilrecord;
import com.woniuxy.caradmin.entity.vo.MaintainrecordVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.MaintainrecordService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author womenzu
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/maintainrecord")
public class MaintainrecordController {
    @Autowired
    private MaintainrecordService maintainrecordService;

    @GetMapping("/maintainrecordList")
    @ApiOperation("查询所有的保养记录")
    public DataGridView selectAllMaintainRecord(MaintainrecordVo maintainrecordVo) {
        Page<Object> page = PageHelper.startPage(maintainrecordVo.getPage(), maintainrecordVo.getLimit());
        List<Maintainrecord> maintainrecords = maintainrecordService.selectAllMaintainRecord(maintainrecordVo);
        if (maintainrecords != null && maintainrecords.size() > 0) {
            return new DataGridView(page.getTotal(), maintainrecords);
        } else {
            throw new CarException(ResultCode.MAINTAINRECORDISNULL, Constaint.MAINTAINRECORDISNULL);
        }
    }

    //1 分页查询
    @GetMapping("/maintainrecords")
    @ApiOperation("分页查询所有的加油记录")
    public DataGridView selectAllBy(MaintainrecordVo maintainrecordVo) {
        Page<Object> page = PageHelper.startPage(maintainrecordVo.getPage(), maintainrecordVo.getLimit());
        List<Maintainrecord> maintainrecords = maintainrecordService.selectAllBy(maintainrecordVo);
        if (maintainrecords != null && maintainrecords.size() > 0) {
            return new DataGridView(page.getTotal(), maintainrecords);
        } else {
            throw new CarException(ResultCode.MAINTAINRECORDISNULL, Constaint.MAINTAINRECORDISNULL);
        }
    }

    //2 增加
    @GetMapping("/addMaintainRecord")
    @ApiOperation("增加保养记录")
    public R addMaintainRecord(Maintainrecord maintainrecord,HttpServletRequest httpServletRequest) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //得到
        String vid = httpServletRequest.getParameter("vid");
        String mtid = httpServletRequest.getParameter("mtid");
        String time = httpServletRequest.getParameter("time");
        String cost = httpServletRequest.getParameter("cost");
        String muid = httpServletRequest.getParameter("muid");
        String nowmileage = httpServletRequest.getParameter("nowmileage");
        String content = httpServletRequest.getParameter("content");
        String nexttime = httpServletRequest.getParameter("nexttime");
        String nextmileage = httpServletRequest.getParameter("nextmileage");
        String information = httpServletRequest.getParameter("information");
        String driverid = httpServletRequest.getParameter("driverid");
        String status = httpServletRequest.getParameter("status");//设置增加的状态
        //时间处理
        java.sql.Date st = null;
        java.sql.Date et = null;
        try {
            Date st1 = sdf.parse(time);
            st = new java.sql.Date(st1.getTime());

            Date et1 = sdf.parse(nexttime);
            et = new java.sql.Date(et1.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //设置
        maintainrecord.setVid(Integer.parseInt(vid));
        maintainrecord.setMtid(Integer.parseInt(mtid));
        maintainrecord.setTime(st);
        maintainrecord.setCost(Double.valueOf(cost));
        maintainrecord.setMuid(Integer.parseInt(muid));
        maintainrecord.setNowmileage(nowmileage);
        maintainrecord.setContent(content);
        maintainrecord.setNexttime(et);
        maintainrecord.setNextmileage(nextmileage);
        maintainrecord.setInformation(information);
        maintainrecord.setDriverid(Integer.parseInt(driverid));
        maintainrecord.setStatus(Integer.parseInt(status));
        int addMaintainRecord = maintainrecordService.addMaintainRecord(maintainrecord);
        if (addMaintainRecord != 0) {
            return R.ok().code(ResultCode.MAINTAINRECORDISSUC).message(Constaint.MAINTAINRECORDISSUC);
        } else {
            throw new CarException(ResultCode.MAINTAINRECORDISFAI, Constaint.MAINTAINRECORDISFAI);
        }
    }
    //3 修改
    @GetMapping("/updateMaintainRecord")
    @ApiOperation("修改保养记录")
    public R updateMaintainRecord(Maintainrecord maintainrecord) {
        int updateMaintainRecord = maintainrecordService.updateMaintainRecord(maintainrecord);
        if (updateMaintainRecord != 0) {
            return R.ok().code(ResultCode.MAINTAINRECORDUPDATEISSUC).message(Constaint.MAINTAINRECORDUPDATEISSUC);
        } else {
            throw new CarException(ResultCode.MAINTAINRECORDUPDATEISFAI, Constaint.MAINTAINRECORDUPDATEISFAI);
        }
    }

    //4 删除
    @GetMapping("/deleteMaintainRecord")
    @ApiOperation("删除保养记录")
    public R deleteMaintainRecord(Integer[] ids) {
        boolean deleteMaintainRecord = maintainrecordService.deleteMaintainRecord(ids);
        if (deleteMaintainRecord) {
            return R.ok().code(ResultCode.MAINTAINRECORDDELETEISSUC).message(Constaint.MAINTAINRECORDDELETEISSUC);
        } else {
            throw new CarException(ResultCode.MAINTAINRECORDDDELETEISFAI, Constaint.MAINTAINRECORDDDELETEISFAI);
        }
    }

    //5 配合修改
    @GetMapping("/maintainRecordById")
    @ApiOperation("根据id查询保养记录")
    public R oilRecordById(Integer id) {
        Maintainrecord maintainRecordById = maintainrecordService.maintainRecordById(id);
        if (maintainRecordById != null) {
            return R.ok().data("maintainRecordById", maintainRecordById);
        } else {
            throw new CarException(ResultCode.MAINTAINRECORDISNULL, Constaint.MAINTAINRECORDISNULL);
        }
    }

}

